﻿
using IRepository;
using IService;
using Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Service
{
    public class BaseService<T> : IBaseService<T> where T : BaseDBModel, new()
    {
        protected IBaseRepository<T> _BaseRepository;
        public BaseService(IBaseRepository<T> BaseRepository)
        {
            _BaseRepository = BaseRepository;
        }

        #region 同步
        /// <summary>
        /// 查找 -通过id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T Find(string id)
        {
            return _BaseRepository.Find(id);
        }

        /// <summary>
        /// 根据条件获取实体对象
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public T Query(Expression<Func<T, bool>> whereLambda)
        {
            return _BaseRepository.Query(whereLambda);
        }

        /// <summary>
        /// 根据条件获取实体对象集合
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public List<T> QueryList(Expression<Func<T, bool>> whereLambda)
        {
            return _BaseRepository.QueryList(whereLambda);
        }
        /// <summary>
        /// 根据条件获取实体对象集合 包括删除数据
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public List<T> QueryListIncludeDelete(Expression<Func<T, bool>> whereLambda)
        {
            return _BaseRepository.QueryListIncludeDelete(whereLambda);
        }
        /// <summary>
        /// 根据条件获取实体对象集合分页
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderbyWhere"></param>
        /// <param name="orderByType"></param>
        /// <returns></returns>
        public (List<T>, int) QueryList(Expression<Func<T, bool>> whereLambda, int pageIndex, int pageSize, Expression<Func<T, object>> orderbyWhere, OrderByType orderByType)
        {
            return _BaseRepository.QueryList(whereLambda, pageIndex,pageSize, orderbyWhere, orderByType);
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public T AddBackEntity(T entity)
        {
            if (string.IsNullOrWhiteSpace(entity.Id))
            {
                entity.Id = Guid.NewGuid().ToString("N");
            }
            entity.IsDelete = false;
            entity.UpdateUser = "";
            entity.DeleteUser = "";
            return _BaseRepository.AddBackEntity(entity);
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Add(T entity)
        {
            if (string.IsNullOrWhiteSpace(entity.Id))
            {
                entity.Id = Guid.NewGuid().ToString("N");
            }
            entity.IsDelete = false;
            entity.UpdateUser = "";
            entity.DeleteUser = "";
            return _BaseRepository.Add(entity);
        }
       
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(T entity)
        {
            return _BaseRepository.Update(entity);
        }
        /// <summary>
        /// 删除 根据条件
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public bool DeleteByWhere(Expression<Func<T, bool>> whereLambda)
        {
            return _BaseRepository.DeleteByWhere(whereLambda);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Dellete(T entity)
        {
            return _BaseRepository.Dellete(entity);
        }
      
        /// <summary>
        /// 查看列表-缓存
        /// </summary>
        /// <returns></returns>
        public List<T> GetListCache()
        {
            return _BaseRepository.GetListCache();
        }

        #endregion

        #region 异步
        /// <summary>
        /// 查找 -通过id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<T> FindAsync(string id)
        {
            return await _BaseRepository.FindAsync(id);
        }

        /// <summary>
        /// 根据条件获取实体对象
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public async Task<T> QueryAsync(Expression<Func<T, bool>> whereLambda)
        {
            return await _BaseRepository.QueryAsync(whereLambda);
        }

        /// <summary>
        /// 根据条件获取实体对象集合
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public async Task<List<T>> QueryListAsync(Expression<Func<T, bool>> whereLambda)
        {
            return await _BaseRepository.QueryListAsync(whereLambda);
        }
        /// <summary>
        /// 根据条件获取实体对象集合 包括删除数据
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public async Task<List<T>> QueryListIncludeDeleteAsync(Expression<Func<T, bool>> whereLambda)
        {
            return await _BaseRepository.QueryListIncludeDeleteAsync(whereLambda);
        }
        /// <summary>
        /// 根据条件获取实体对象集合分页
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="orderbyWhere"></param>
        /// <param name="orderByType"></param>
        /// <returns></returns>
        public async Task<(List<T>, int)> QueryListAsync(Expression<Func<T, bool>> whereLambda, int pageIndex, int pageSize, Expression<Func<T, object>> orderbyWhere, OrderByType orderByType)
        {
            return await _BaseRepository.QueryListAsync(whereLambda, pageIndex, pageSize, orderbyWhere, orderByType);
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<T> AddBackEntityAsync(T entity)
        {
            if (string.IsNullOrWhiteSpace(entity.Id))
            {
                entity.Id = Guid.NewGuid().ToString("N");
            }
            entity.IsDelete = false;
            entity.UpdateUser = "";
            entity.DeleteUser = "";
            return await _BaseRepository.AddBackEntityAsync(entity);
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> AddAsync(T entity)
        {
            if (string.IsNullOrWhiteSpace(entity.Id))
            {
                entity.Id = Guid.NewGuid().ToString("N");
            }
            entity.IsDelete = false;
            entity.UpdateUser = "";
            entity.DeleteUser = "";
            return await _BaseRepository.AddAsync(entity);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(T entity)
        {
            return await _BaseRepository.UpdateAsync(entity);
        }
        /// <summary>
        /// 删除 根据条件
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public async Task<bool> DeleteByWhereAsync(Expression<Func<T, bool>> whereLambda)
        {
            return await _BaseRepository.DeleteByWhereAsync(whereLambda);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> DelleteAsync(T entity)
        {
            return await _BaseRepository.DelleteAsync(entity);
        }

        /// <summary>
        /// 查看列表-缓存
        /// </summary>
        /// <returns></returns>
        public async Task<List<T>> GetListCacheAsync()
        {
            return await _BaseRepository.GetListCacheAsync();
        }

        #endregion
    }
}
